package com.memo4android;

import com.memo4android.model.AppModel;
import com.memo4android.model.Dossier;
import com.memo4android.model.ItemNote;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class NewNoteActivity extends Activity implements OnClickListener {
	private Dossier dossier;
	private Spinner spinner;
	private AppModel model;
	private int positionDefault=-1;
	private ArrayAdapter<CharSequence> m_adapterForSpinner;
	private String folderName;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frame_addnotev2);
        setTitle(this.getTitle() + " - New note");
        
        model = AppModel.getInstance(this);
        folderName = getIntent().getStringExtra("folderName");
        dossier = model.getDossier(folderName);
      
        Button valid=(Button) findViewById(R.id.button_validNewNote);
        valid.setOnClickListener(this);
        Button cancel=(Button) findViewById(R.id.button_CancelNewNote);
        cancel.setOnClickListener(this);
        
        spinner=(Spinner) findViewById(R.id.spinnerNewNote);
        
        m_adapterForSpinner = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
        m_adapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(m_adapterForSpinner);
		
        int i;
        for(i=0;i<model.getNumberOfDossiers();i++){
        	m_adapterForSpinner.add(model.getDossier(i).getTitre().toString());
        	if(model.getDossier(i).equals(dossier)){
        		positionDefault=i;
        	}
		}
        m_adapterForSpinner.add(model.getHome().getTitre().toString());
      
        
        //selection du spinner par default home sinon le dossier en question
        if(positionDefault==-1){
        	positionDefault=i+1;
        }
        
        spinner.setSelection(positionDefault);
  	}
	
    @Override
    public void onBackPressed(){
    	if(folderName.equals("__HOME")){
			Intent intent = new Intent(this ,MainView.class);
			this.startActivity(intent);  
    	} else {
			Intent intent = new Intent(this ,InFolderActivity.class);
			intent.putExtra("folderName", folderName);
			this.startActivity(intent);      		
    	}
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.button_validNewNote:
			String folderSelected=spinner.getSelectedItem().toString();
			dossier=model.getDossier(folderSelected);
	        if(dossier != null){ 
	        	TextView titleText = (TextView) findViewById(R.id.titreNewNote);
	        	String title=titleText.getText().toString();
	        	TextView contentText = (TextView) findViewById(R.id.corpsNewNote);
	        	String content=contentText.getText().toString();
	        	
	        	model.addNote(dossier, new ItemNote(dossier,title,content));
	        	Intent intent;
	        	if(dossier.isHome())
	        		intent = new Intent(this, MainView.class);
	        	else {
	        		intent = new Intent(this, InFolderActivity.class);
	        		intent.putExtra("folderName", dossier.getTitre());
	        	}
	        	startActivity(intent);
	        }
			break;
		case R.id.button_CancelNewNote:
			this.finish();
			break;
		}
		
	}
	
}
